package cn.edu.scau.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.scau.entity.Work;
import cn.edu.scau.service.WorkService;

@Controller
@RequestMapping("/Work")
public class WorkServiceTest {

	// 业务逻辑组件
	@Autowired
	private WorkService workService;

	public WorkService getWorkService() {
		return workService;
	}

	public void setWorkService(WorkService workService) {
		this.workService = workService;
	}

	@RequestMapping("/jump")
	public String jump() {

		System.out.println("asijdflas");
		return "upload";
	};

	@SuppressWarnings("unchecked")
	@RequestMapping("/save")
	public String save(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) {

		Work works = new Work();

		works.setColumnId(1);
		works.setEditor("caimingwei");
		works.setFirstAuthor("caimingwei");
		works.setGrade("大二");
		works.setIntro("非常的好");
		works.setKind("自然科学");
		works.setLevel("1");
		works.setLikeTimes(90);
		works.setPicture("isjhdlfjasdf");
		works.setPoster("skddjhflksddff");
		works.setProfession("数信学院");
		works.setReleaseDate("2013-07-11 0:05:19");
		works.setStatus(true);
		works.setTeam("菜菜");
		works.setTitle("非常好的实验");

		this.getWorkService().saveImage(files, request, "2013-03-02 01:05:03");
		this.getWorkService().add(works);

		return "NewFile";
	};

	@SuppressWarnings("unchecked")
	@RequestMapping("/delete")
	public String delete(@RequestParam(name = "id") Integer id) {

		this.getWorkService().delete(id);

		return "NewFile";
	};

	@SuppressWarnings("unchecked")
	@RequestMapping("/update")
	public String update() {

		Work works = (Work) this.getWorkService().getWorkByID(2);
		works.setColumnId(200);
		this.getWorkService().update(works,works.getWork_id());

		return "NewFile";
	};

	@ResponseBody
	@RequestMapping("/front")
	public List findByPageFront() {

		System.out.println(this.getWorkService().countTheEntityNum(1, 5, true));
		return this.getWorkService().getPropertyOfWork(18, 1, 100, true);
	};

	@ResponseBody
	@RequestMapping("/management")
	public List findByPageManagement() {
		System.out.println(this.getWorkService().countTheEntityNum(1, 5, false));
		return this.getWorkService().getPropertyOfWork(18, 1, 10, false);
	};

}
